

import java.sql.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.util.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class InitialParam extends JFrame {

    static  Connection conn = null;
    PreparedStatement ps = null;
    ResultSet res = null;

    //**********************数据列表**********************
    Vector columnNames = null;
    Vector<Vector<String>> rowData = null;
    //**********************数据列表**********************


    public  Vector<Vector<String>> getStudent(Connection conn) throws SQLException {
        rowData = new Vector<>();
        ps = conn.prepareStatement("SELECT * FROM student");
        res = ps.executeQuery();
        while (res.next())
        {
            Vector hang = new Vector();
            hang.add(res.getString(1));
            hang.add(res.getString(2));
            hang.add(res.getString(3));
            hang.add(res.getString(4));
            hang.add(res.getString(5));
            hang.add(res.getString(6));
            rowData.add(hang);
        }
        System.out.println("数据获取成功");
        return rowData;
    }

    public  Vector<Vector<String>> getCourse(Connection conn) throws SQLException {
        rowData = new Vector<>();
        ps = conn.prepareStatement("SELECT * FROM course");
        res = ps.executeQuery();
        while (res.next())
        {
            Vector hang = new Vector();
            hang.add(res.getString(1));
            hang.add(res.getString(2));
            hang.add(res.getString(3));
            hang.add(res.getString(4));
            hang.add(res.getString(5));
            rowData.add(hang);
        }
        System.out.println("数据获取成功");
        return rowData;
    }

    public  Vector<Vector<String>> getSc(Connection conn) throws SQLException {
        rowData = new Vector<>();
        ps = conn.prepareStatement("SELECT * FROM sc");
        res = ps.executeQuery();
        while (res.next())
        {
            Vector hang = new Vector();
            hang.add(res.getString(1));
            hang.add(res.getString(2));
            hang.add(res.getString(3));
            hang.add(res.getString(4));
            rowData.add(hang);
        }
        System.out.println("数据获取成功");
        return rowData;
    }


    //获取数据库操作流
    public Connection getConn() {
        try {
            String dbURL = "jdbc:sqlserver://localhost:1434;databasename=sy7"; // 1433是端口，"STUDB"是数据库名称
            String userName = "sa"; // 用户名
            String userPwd = "r123456"; // 密码
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(dbURL,userName,userPwd);
            System.out.println("数据库连接成功");
            JOptionPane.showConfirmDialog(null,"数据库连接成功！","提示",JOptionPane.OK_OPTION);
            return conn;
        }catch (Exception q){
            q.printStackTrace();
            System.out.println("连接数据库失败");
            return  null;
        }

    }

//    public static void main(String[] args) throws SQLException {
//        test2 L= new test2();
//        Vector<Vector<String>> v  = L.getStudent(L.getConn());
//        for(Vector<String> s:v){
//            System.out.println();
//           for(String i:s){
//               System.out.print(i+"  ");
//           }
//        }
//
//    }

}









